﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TakaGUI;
using EvoSim;
using TakaGUI.DrawBoxes;
using EvoApp.Menu.Forms;
using TakaGUI.DrawBoxes.Forms;
using EvoApp.DrawBoxes;

namespace EvoApp.Menu
{
	public class Editor : SingleSlotBox
	{
		public Simulation Simulation;

		public VerticalSplitPanel VSplit;

		public MenuBar MenuBar;
		public WorldBox WorldBox;

		public void Initialize()
		{
			base.BaseInitialize();
		}

		public override void AddedToContainer()
		{
			base.AddedToContainer();

			FillWidth();
			FillHeight();

			setUpMenuBar();

			VSplit = new VerticalSplitPanel();
			VSplit.Initialize();
			AddDrawBox(VSplit);
			VSplit.Y = MenuBar.Y + MenuBar.Height;
			VSplit.FillWidth();
			VSplit.Height = GetDefaultBoundaries().Height - VSplit.Y;

			setUpWorldBox();
		}

		void setUpMenuBar()
		{
			MenuBar = new MenuBar();
			MenuBar.Initialize();
			AddDrawBox(MenuBar);
			MenuBar.FillWidth();
			MenuBar.Height = 40;

			setUpFileMenuItem();
			setUpCreaturesMenuItem();
		}
		//File
		void setUpFileMenuItem()
		{
			MenuBar.MenuItem menuItem = new MenuBar.MenuItem("File");
			MenuBar.MenuItems.Add(menuItem);
			{
				menuItem.Elements.Add(new MenuBar.MenuItem.MenuElement("New Simulation", delegate(object sender)
				{
					if (Simulation != null)
					{
						YesNoAbortForm.ShowDialogue(Parent, null, "Do you want to save the current simulation?", delegate(object _sender)
						{
							var dialogue = (TakaGUI.DrawBoxes.Forms.YesNoAbortForm)_sender;

							if (dialogue.DialogResult == DialogResult.Yes)
								Simulation.Save();

							if (dialogue.DialogResult != DialogResult.Abort)
								showNewSimulationForm();
						});
					}
					else
					{
						showNewSimulationForm();
					}
				}));

				menuItem.Elements.Add(new MenuBar.MenuItem.MenuElement("Open Simulation", delegate(object sender)
				{
					if (Simulation != null)
					{
						YesNoAbortForm.ShowDialogue(Parent, null, "Do you want to save the current simulation?", delegate(object _sender)
						{
							var dialogue = (TakaGUI.DrawBoxes.Forms.YesNoAbortForm)_sender;

							if (dialogue.DialogResult == DialogResult.Yes)
								Simulation.Save();

							if (dialogue.DialogResult != DialogResult.Abort)
								showOpenSimulationForm();
						});
					}
					else
					{
						showOpenSimulationForm();
					}
				}));

				menuItem.Elements.Add(new MenuBar.MenuItem.MenuElement("Save Simulation", delegate(object sender)
				{
					Simulation.Save();
				}));
			}
		}
		void showNewSimulationForm()
		{
			NewSimulationForm.ShowDialog(Parent, delegate(object sender)
			{
				var dialogue = (NewSimulationForm)sender;
				if (dialogue.Result != null)
				{
					Simulation = dialogue.Result;

					WorldBox.World = Simulation.World;
				}
			});
		}
		void showOpenSimulationForm()
		{
			FileForm.ShowDialogue(Parent, FileForm.FileFormTypes.File, FileForm.OperationTypes.Open, delegate(object sender)
			{
				var fileForm = (FileForm)sender;

				if (fileForm.Result == DialogResult.OK)
					Simulation = Simulation.Load(fileForm.SelectedFile.FullName);
			});
		}
		//Creatures
		void setUpCreaturesMenuItem()
		{

		}

		void setUpWorldBox()
		{
			WorldBox = new WorldBox();
			WorldBox.Initialize();
			VSplit.RightPanel.AddDrawBox(WorldBox);

			VSplit.UpdateSize();

			WorldBox.Fill();
		}

		public override void Idle(Microsoft.Xna.Framework.GameTime gameTime)
		{
			base.Idle(gameTime);
		}

		public override TakaGUI.Services.ViewRect GetDefaultBoundaries(int newWidth, int newHeight)
		{
			return new TakaGUI.Services.ViewRect(RealX, RealY, newWidth, newHeight);
		}
	}
}
